﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Web;
using System.Web.Http;
using Util.Security;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using FSLib.Network.Http;
using Newtonsoft.Json.Linq;
using SecurityClass;

namespace SecurityTest.Controllers
{
    public class UserController : ApiController
    {
        
        [HttpGet]
        public IHttpActionResult Login()
        {

            IIdentity identity = new Identity(true, "fanlin.zeng");
            IPrincipal principal = new Principal(identity);
            System.Web.HttpContext.Current.User = principal;

            Principal user = SecurityContext.User;

            return Json(user);
        }
        string client_id = "df1d0922cf5d515ad2aeb286bd0ba45e79d2142e54ed2e19b7ee64318592dd1e";
        string redirect_uri = "http://localhost:50453/api/user/getcode";
        string client_secret = "c78757ed08145548fa43be5ab15682a7ecb2fdc426a693ffd43bd70dfec4f657";
        [HttpGet]
        public IHttpActionResult GetCode([FromUri]string code)
        {
            string result = string.Empty; string url = "https://gitee.com/oauth/token";
            HttpContent httpContent = new FormUrlEncodedContent(new Dictionary<string, string>()
           {
              {"grant_type", "authorization_code"},
              {"code", code},
              {"client_id", client_id},
              {"redirect_uri", redirect_uri},
              {"client_secret", client_secret},
           });
            RequestData requestdata = new RequestData
            {
                grant_type = "authorization_code",
                code = code,
                client_id = client_id,
                redirect_uri = redirect_uri,
                client_secret = client_secret,
            };
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            Dictionary<string, string> keyValues = new Dictionary<string, string>();
            keyValues["grant_type"] = "authorization_code";
            keyValues["code"] = code;
            keyValues["client_id"] = client_id;
            keyValues["redirect_uri"] = redirect_uri;
            keyValues["client_secret"] = client_secret;
            FormUrlEncodedContent content = new FormUrlEncodedContent(keyValues);
            var respMsg =  client.PostAsync(url, content).Result;// 不要错误的调用 了 PutAsync，应该是 PostAsync 
            string msgBody =  respMsg.Content.ReadAsStringAsync().Result; 
            //string content = JsonConvert.SerializeObject();
            //HttpContent httpContent = new StringContent(postContent);
            //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            //httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            //HttpClient httpClient = new HttpClient();

            //HttpResponseMessage response = httpClient.PostAsync(redirect_uri, httpContent).Result;

            //if (response.IsSuccessStatusCode)
            //{
            //    result = response.Content.ReadAsStringAsync().Result;
            //}
            //FSLib.Network.Http.HttpClient flshclient = new FSLib.Network.Http.HttpClient();

            //FSLib.Network.Http.HttpContext Context = flshclient.Create<string>(
            //    FSLib.Network.Http.HttpMethod.Post,
            //    url,
            //    data: requestdata,
            //    contentType: FSLib.Network.Http.ContentType.FormUrlEncoded);

            //FSLib.Network.Http.HttpContext fishclientcontext = Context.Send();
            //if (fishclientcontext.IsSuccess)
            //{
            //    result =JsonConvert.SerializeObject( fishclientcontext.WebResponse);
            //}
            return Ok();
        }
        [HttpPost]
        public IHttpActionResult Gettoken(RequestData req)
        {
            return Json(req);
        }



    }

    
 

}
